package com.pptv.mtbu.ottflow.action;

import java.util.LinkedList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import com.opensymphony.xwork2.ActionContext;
import com.pptv.mtbu.ottflow.action.base.AdminBaseAction;
import com.pptv.mtbu.ottflow.domain.Role;
import com.pptv.mtbu.ottflow.domain.User;
import com.pptv.mtbu.ottflow.service.AdminManager;
import com.pptv.mtbu.ottflow.vo.PageBean;

public class UserAction
    extends AdminBaseAction
{

    /**
     * 
     */
    private static final long serialVersionUID = -734935186759162931L;

    static Logger logger = LoggerFactory.getLogger(UserAction.class);
    
    
    /**
     * 显示的用户
     */
    private List<User> users;
    
    /**
     * 显示的角色
     */
    private List<Role> roles;
    
    
    /**
     * 搜索 参数
     */
    private String searchKey="";
    
    /**
     * roleId 参数
     */
    private String roleId = "";
    
    /**
     * pageIdx 参数
     */
    private String pageIdx="";
    
    /**
     * pageSize
     */
    private int pageSize;
    
    /**
     * 当前页面一共有多少结果
     */
    private int recordCount;
    
    /**
     * for view
     * @param users
     */
    public void setUsers(List<User> users)
    {
        this.users = users;
    }
    public List<User> getUsers()
    {
        return this.users;
    }
    
    

    public String execute()
            throws Exception
    {
        logger.info("execute");
    
        
        //创建ActionContext实例
        //ActionContext ctx = ActionContext.getContext();
   
        //获取HttpSession中的user属性
      //  String user = (String)ctx.getSession().get(WebConstant.USER);
        
        /**
         * 分页
         */
        PageBean pageBean = new PageBean(getPageIdx(),getPageSize());
        
        
        
        String hql = "From User as usr ";
        String where = "";
        List<Object> objs = new LinkedList<Object>();
        
        
        
        if(getRoleId().equals("") || getRoleId().equals("0")){
        	
        }
        else{
        	Role role = new Role();
        	role.setId(Integer.parseInt(getRoleId(),10));
        	
        	if(where.equals("") == false ) where += " and ";
        	where += "usr.role=?";
        	objs.add(role);
        }
        
        if(getSearchKey().equals("") ){
        	
        }
        else{
        	if(where.equals("") == false ) where += " and ";
        	where += "usr.UserName=?";
        	objs.add(getSearchKey());
        }
        
        if(where.equals("") == false) where = " where " + where;
        
        
        List<User> users = mgr.find(hql + where, objs.toArray(), pageBean);
        List<Role> roles = mgr.findAllRoles();  
        
        
        setUsers(users);
        setRoles(roles);
        
        setRecordCount(mgr.findUsrNum(hql + where, objs.toArray()));
        return SUCCESS;
    }
    public String getSearchKey() {
        return searchKey;
    }
    public void setSearchKey(String searchKey) {
        this.searchKey = searchKey;
    }
    public String getRoleId() {
        return roleId;
    }
    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }
    public int getPageIdx() {
        int pageIdx;
        try{
            pageIdx = Integer.parseInt(this.pageIdx, 10);
        }
        catch(Exception e){
            pageIdx = 0;
        }
        return pageIdx;
    }
    public void setPageIdx(String pageIdx) {
        logger.info("" + pageIdx);
        try{
            Integer.parseInt(pageIdx, 10);
        }
        catch(Exception e){
            pageIdx = "0";
        }
        this.pageIdx = pageIdx;
    }
    public int getPageSize() {
        if(pageSize <= 0){
            pageSize = 2;
        }
        return pageSize;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getRecordCount() {
        return recordCount;
    }
    public void setRecordCount(int recordCount) {
        this.recordCount = recordCount;
    }
    public List<Role> getRoles() {
        return roles;
    }
    public void setRoles(List<Role> roles) {
        this.roles = roles;
    }
}
